<template>
    <view class="modal-container" v-if="visible" @click.stop="handleMaskClick">
        <view class="modal-mask"></view>
        <view class="modal-content" @click.stop>
            <view class="modal-title">{{ title }}</view>
            <view class="modal-body">
                <text>{{ content }}</text>
            </view>
            <view class="modal-footer">
                <view class="modal-btn cancel" v-if="showCancel" @click="handleCancel">{{ cancelText }}</view>
                <view class="modal-btn confirm" @click="handleConfirm">{{ confirmText }}</view>
            </view>
        </view>
    </view>
</template>

<script lang="ts" setup>
import { ref, defineProps, defineEmits } from 'vue';

const props = defineProps({
    title: {
        type: String,
        default: '提示'
    },
    content: {
        type: String,
        default: ''
    },
    confirmText: {
        type: String,
        default: '确定'
    },
    cancelText: {
        type: String,
        default: '取消'
    },
    showCancel: {
        type: Boolean,
        default: true
    },
    maskClosable: {
        type: Boolean,
        default: false
    }
});

const visible = ref(false);
const emit = defineEmits(['confirm', 'cancel']);

function open() {
    visible.value = true;
}

function close() {
    visible.value = false;
}

function handleConfirm() {
    close();
    emit('confirm');
}

function handleCancel() {
    close();
    emit('cancel');
}

function handleMaskClick() {
    if (props.maskClosable) {
        close();
        emit('cancel');
    }
}

defineExpose({
    open,
    close
});
</script>

<style lang="scss" scoped>
.modal-container {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-mask {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.6);
}

.modal-content {
    position: relative;
    width: 80%;
    background-color: #fff;
    border-radius: 12px;
    overflow: hidden;
    animation: modalFadeIn 0.3s ease;
}

@keyframes modalFadeIn {
    from {
        opacity: 0;
        transform: scale(0.9);
    }

    to {
        opacity: 1;
        transform: scale(1);
    }
}

.modal-title {
    font-size: 18px;
    font-weight: bold;
    text-align: center;
    padding: 15px;
    color: #333;
}

.modal-body {
    padding: 10px 20px 20px;
    text-align: center;
    color: #666;
    font-size: 16px;
}

.modal-footer {
    display: flex;
    border-top: 1px solid #eee;
}

.modal-btn {
    flex: 1;
    height: 45px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    transition: background-color 0.2s;

    &.cancel {
        color: #666;
        border-right: 1px solid #eee;

        &:active {
            background-color: #f5f5f5;
        }
    }

    &.confirm {
        color: #2196f3;
        font-weight: 500;

        &:active {
            background-color: #e6f7ff;
        }
    }
}
</style>